package ezXpns.GUI;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import javax.swing.JPanel;
import javax.swing.JLabel;
import org.mcavallo.opencloud.Cloud;
import org.mcavallo.opencloud.Tag;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Container;

/**
 * @author A0086931X
 */

//@author A0086931X

public class CloudPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	boolean debug = true;
	private HashMap<String, Float> tagHashMap;
	private JLabel label;
	private ArrayList<JLabel> displayArray;

	/**
	 * Create the panel.
	 */
	private Cloud cloud;

	public CloudPanel() {
		WrapLayout wl_panel = new WrapLayout(0);
		setLayout(wl_panel);
		Container test = new Container();
		test.setBounds(0, 0, 10, 10);
		wl_panel.preferredLayoutSize(test);
		setBorder(new LineBorder(new Color(0, 0, 0)));

		displayArray = new ArrayList<JLabel>();

		cloud = new Cloud();

		cloud.setMaxTagsToDisplay(30);
		cloud.setMaxWeight(10);
		cloud.setMinWeight(3);
		cloud.tags(new Tag.ScoreComparatorDesc());

	}

	public void setTagHashMap(HashMap<String, Float> tagHashMap) {
		this.tagHashMap = tagHashMap;
		generateCloud();
	}

	public void clearDisplay() {
		for (JLabel item : displayArray) {
			remove(item);
			cloud.clear();
		}
	}

	public void generateCloud() {
		clearDisplay();

		for (Entry<String, Float> entry : tagHashMap.entrySet()) {
			cloud.addTag(new Tag(entry.getKey(), entry.getValue()));
		}

		for (Tag tag : cloud.tags(new Tag.ScoreComparatorDesc())) {
			label = new JLabel(tag.getName());
			label.setOpaque(true);
			label.setFont(label.getFont().deriveFont((float) tag.getWeight() * 5));

			add(label);
			displayArray.add(label);
		}
	}
}
